package com.ruoyi.business.tools;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itextpdf.text.DocumentException;
import com.ruoyi.business.domain.Employee;
import com.ruoyi.business.domain.vo.EmployeePdf;
import com.ruoyi.business.service.IBedsPlusService;
import com.ruoyi.business.service.IEmployeePlusService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component("ywTask")
public class ywTask {
    @Autowired
    private  IEmployeePlusService employeePlusService;
    @Autowired
    private  IBedsPlusService bedsPlusService;
    /*
     * 定时生成员工pdf
     * */
    public void addemployeePdf() throws DocumentException, IOException {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(Employee::getPdfurl);
        List<Employee> list = employeePlusService.list(queryWrapper);
        pdfTool pdfTool=new pdfTool();
        for (Employee entity : list) {
            if (entity.getRoomNumber()!=null&&!entity.getRoomNumber().isEmpty()){
                entity.setBeddetail(bedsPlusService.getbedDetailByid(entity.getRoomNumber()));
            }
            EmployeePdf employeePdf = new EmployeePdf();
            if (entity.getEntryDate()!=null) {
                employeePdf.setEntryDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entity.getEntryDate()));
            }
            if (entity.getAge() != null) {
                employeePdf.setAge(entity.getAge());
            } else {
                employeePdf.setAge(0L); // 如果 age 为 null，将其设置为默认值，这里设置为 0
            }
            employeePdf.setJobNumber(entity.getJobNumber());
            employeePdf.setFullName(entity.getFullName());
            employeePdf.setDepartment(entity.getDepartment());
            employeePdf.setPosition(entity.getPosition());
            employeePdf.setGender(entity.getGender());
            employeePdf.setEthnicity(entity.getEthnicity());
            employeePdf.setIdNumber(entity.getIdNumber());
            employeePdf.setHomeAddress(entity.getHomeAddress());
            employeePdf.setResidentialAddress(entity.getResidentialAddress());
            employeePdf.setContactNumber(entity.getContactNumber());
            employeePdf.setEmergencyContactPerson(entity.getEmergencyContactPerson());
            employeePdf.setEmergencyContactNumber(entity.getEmergencyContactNumber());
            employeePdf.setEducation(entity.getEducation());
            employeePdf.setMajor(entity.getMajor());
            employeePdf.setTechnicalTitle(entity.getTechnicalTitle());
            employeePdf.setAccommodation(entity.getAccommodation());
            //employeePdf.setRoomNumber(entity.getRoomNumber());
            employeePdf.setBeddetail(entity.getBeddetail());
            employeePdf.setFreeMeal(entity.getFreeMeal());
            employeePdf.setJobchange(entity.getJobchange());
            employeePdf.setSecondemployment(entity.getSecondemployment());
            employeePdf.setPositionLevel(entity.getPositionLevel());
            employeePdf.setFujianmanage(entity.getFujianmanage());
            if (entity.getInsuranceStartDate()!=null) {
                employeePdf.setInsuranceStartDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM, entity.getInsuranceStartDate()));
            }
            if (entity.getInsuranceEndDate()!=null) {
                employeePdf.setInsuranceEndDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM, entity.getInsuranceEndDate()));
            }
            if (entity.getHousingFundStartDate()!=null) {
                employeePdf.setHousingFundStartDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM, entity.getHousingFundStartDate()));
            }

            employeePdf.setRuralMedicalInsurance(entity.getRuralMedicalInsurance());
            employeePdf.setInsurancePacific(entity.getInsurancePacific());
            employeePdf.setInsuranceCpic(entity.getInsuranceCpic());
            employeePdf.setInsuranceChinalife(entity.getInsuranceChinalife());
            employeePdf.setWaiveSocialInsurance(entity.getWaiveSocialInsurance());
            employeePdf.setSocialInsurance(entity.getSocialInsurance());
            if (entity.getFirstContractExpiryDate()!=null) {
                employeePdf.setFirstContractExpiryDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entity.getFirstContractExpiryDate()));
            }
            if (entity.getSecondContractExpiryDate()!=null) {
                employeePdf.setSecondContractExpiryDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entity.getSecondContractExpiryDate()));
            }
            if (entity.getThirdContractExpiryDate()!=null) {
                employeePdf.setThirdContractExpiryDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entity.getThirdContractExpiryDate()));
            }
            if (entity.getFourthContractExpiryDate()!=null) {
                employeePdf.setFourthContractExpiryDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entity.getFourthContractExpiryDate()));
            }
            if (entity.getFifthContractExpiryDate()!=null) {
                employeePdf.setFifthContractExpiryDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entity.getFifthContractExpiryDate()));
            }
            if (entity.getDepartureDate()!=null) {
                employeePdf.setDepartureDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entity.getDepartureDate()));
            }
            employeePdf.setDepartureStatus(entity.getDepartureStatus());
            employeePdf.setDepartureReason(entity.getDepartureReason());
            employeePdf.setImg(entity.getImg());
            employeePdf.setCrime(entity.getCrime());
            employeePdf.setRemarks(entity.getRemarks());
            employeePdf.setContractRemarks(entity.getContractRemarks());
           Long filename= System.currentTimeMillis();
            if (pdfTool.creatPdf(employeePdf,filename)){
                entity.setPdfurl(filename+".pdf");
            }
        }
        employeePlusService.updateBatchById(list);
    }

    /*
     * 定时生成员工pdf
     * */
    public void updateAge(){
        employeePlusService.updateAge();
    };
}
